//
//  testcardSSView.m
//  testcardSS
//
//  Created by Qiushi on 11-7-26.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "testcardSSView.h"

@implementation testcardSSView


- (id)initWithFrame:(NSRect)frame isPreview:(BOOL)isPreview
{
    self = [super initWithFrame:frame isPreview:isPreview];
    
    if (self) {
        
        /* Initialization */
        
        [self setAnimationTimeInterval:1]; // one second
        
        timerBlockTextDateFormatter = [[NSDateFormatter alloc] init];
        [timerBlockTextDateFormatter setDateFormat: @"HH:mm:ss"];
        
        testCardImage = [[NSImage alloc] 
                         initWithContentsOfURL: 
                            [NSURL URLWithString: 
                                @"http://img6.imageshack.us/img6/3157/pm55441440x1080.gif"]];
        
    }
    
    return self;
}

- (void) dealloc
{
    [super dealloc];
    
    [timerBlockTextDateFormatter release];
    [testCardImage release];
}

- (void)startAnimation
{
    [super startAnimation];
}

- (void)stopAnimation
{
    [super stopAnimation];
}

- (void)drawRect:(NSRect)rect
{
    [super drawRect:rect];
    
    /* 
     
     The reason the positioning is set up in drawRect is that in the situation of dual-monitors, 
     positioning calculation in initWithFrame will result in positiong errors of the slave monitor. 
     
     */
    
    /* Positioning */
    
    float onScreenTestCardImageWidth = [testCardImage size].width / [testCardImage size].height * rect.size.height;
    
    float onScreenTestCardImageX = rect.origin.x + 0.5 * (rect.size.width - onScreenTestCardImageWidth);
    
    float timerBlockX = 580.0 / 1440.0 * onScreenTestCardImageWidth + rect.origin.x + 0.5 * (rect.size.width - onScreenTestCardImageWidth);
    
    float timerBlockY = rect.size.height - 840.0 * rect.size.height / 1080.0 - 40.0 * rect.size.height / 1080.0;
    
    float timerBlockWidth = 460.0 * onScreenTestCardImageWidth / 1440.0;
    
    float timerBlockHeight = 60.0 * rect.size.height / 1080.0;
    
    
    /* Rects */
    
    onScreenTestCardImageRect = NSMakeRect(onScreenTestCardImageX, 0, onScreenTestCardImageWidth, rect.size.height);
    
    timerBlockRect = NSMakeRect(timerBlockX, timerBlockY, timerBlockWidth, timerBlockHeight);
    
    
    /* Font Definitions */
    
    timerBlockTextFontSize = 60 * rect.size.height / 1080;
    
    timerBlockTextFont = [NSFont fontWithName:@"Lucida Grande" size: timerBlockTextFontSize] ;
    
    timerBlockTextFontAttributes = [NSDictionary dictionaryWithObjectsAndKeys:timerBlockTextFont, 
                                    NSFontAttributeName, 
                                    [NSColor whiteColor], 
                                    NSForegroundColorAttributeName, 
                                    nil];
    
    [self startAnimation];

}

- (void)animateOneFrame
{

    /* Timer Block Text Definition*/

    NSString *timerBlockTextString = [timerBlockTextDateFormatter stringFromDate:[NSDate date]];
    
    /* Drawing */
    [testCardImage drawInRect:onScreenTestCardImageRect fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0];
    
    [timerBlockTextString drawInRect:timerBlockRect withAttributes: timerBlockTextFontAttributes];

}

- (BOOL)hasConfigureSheet
{
    return YES;
}

- (void) closeConfigureSheet
{
    [NSApp endSheet: configureWindow];
    [configureWindow orderOut: self];
    [configureWindow release];
}


- (NSWindow*)configureSheet
{
    //if(configureWindow) [configureWindow release];  // Create a new configure Sheet instance every time it loads.
    
    
    NSRect windowRect = NSMakeRect(10.0f, 10.0f, 400.0f, 300.0f);
    configureWindow = [[NSWindow alloc] 
                        initWithContentRect:windowRect 
                        styleMask:(NSResizableWindowMask|NSClosableWindowMask|NSTitledWindowMask) 
                        backing: NSBackingStoreBuffered defer:NO];
    
    NSView *myView = [[NSView alloc] initWithFrame: [configureWindow frame]];
    
    NSRect buttonRect = NSMakeRect(50.0f, 50.0f, 200.0f, 50.0f);
    NSButton *myCloseButton = [[NSButton alloc] initWithFrame: buttonRect];
    [myCloseButton setTitle: @"Close this window"];
    [myCloseButton setAction:@selector(closeConfigureSheet)];
    [myCloseButton setTarget:self];
    [myCloseButton setButtonType:NSMomentaryPushInButton];
    [myCloseButton setBezelStyle:NSRoundedBezelStyle];
    [myView addSubview: myCloseButton];
    [configureWindow setContentView: myView];

    return configureWindow;

}

@end
